package com.nexen.merchant.city.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.nexen.common.annotation.Anonymous;
import com.nexen.common.core.domain.entity.SysUser;
import com.nexen.common.core.domain.model.LoginUser;
import com.nexen.common.utils.SecurityUtils;
import com.nexen.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nexen.common.annotation.Log;
import com.nexen.common.core.controller.BaseController;
import com.nexen.common.core.domain.AjaxResult;
import com.nexen.common.enums.BusinessType;
import com.nexen.merchant.city.domain.NexenCity;
import com.nexen.merchant.city.service.INexenCityService;
import com.nexen.common.utils.poi.ExcelUtil;
import com.nexen.common.core.page.TableDataInfo;

/**
 * 城市管理Controller
 *
 * @author xiao
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/merchant/city")
public class NexenCityController extends BaseController
{
    @Autowired
    private INexenCityService nexenCityService;

    /**
     * 查询城市管理列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexenCity nexenCity)
    {
        startPage();
        List<NexenCity> list = nexenCityService.selectNexenCityList(nexenCity);
        return getDataTable(list);
    }

    /**
     * 查询城市管理列表
     * 不用权限验证该接口
     */
    @Anonymous
    @GetMapping("/list/all")
    public AjaxResult listAll(NexenCity nexenCity)
    {
        List<NexenCity> list = nexenCityService.selectNexenCityList(nexenCity);
        return success(list);
    }

    /**
     * 导出城市管理列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:export')")
    @Log(title = "城市管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexenCity nexenCity)
    {
        List<NexenCity> list = nexenCityService.selectNexenCityList(nexenCity);
        ExcelUtil<NexenCity> util = new ExcelUtil<NexenCity>(NexenCity.class);
        util.exportExcel(response, list, "城市管理数据");
    }

    /**
     * 获取城市管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return success(nexenCityService.selectNexenCityById(Id));
    }

    /**
     * 新增城市管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:add')")
    @Log(title = "城市管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexenCity nexenCity)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenCity.setCreateBy(sysUser.getUserName());
        }
        return toAjax(nexenCityService.insertNexenCity(nexenCity));
    }

    /**
     * 修改城市管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:edit')")
    @Log(title = "城市管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexenCity nexenCity)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)){
            SysUser sysUser = loginUser.getUser();
            nexenCity.setUpdateBy(sysUser.getUserName());
        }
        return toAjax(nexenCityService.updateNexenCity(nexenCity));
    }

    /**
     * 删除城市管理
     */
    @PreAuthorize("@ss.hasPermi('merchant:city:remove')")
    @Log(title = "城市管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(nexenCityService.deleteNexenCityByIds(Ids));
    }
}
